﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class CharactorListController
{
    private List<CharacterData> _datas;
    // UXML template for list entries
    VisualTreeAsset m_ListEntryTemplate;

// UI element references
    ListView m_CharacterList;
    Label m_CharClassLabel;
    Label m_CharNameLabel;
    VisualElement m_CharPortrait;
    Button m_SelectCharButton;
    public void InitializeCharacterList(VisualElement root, VisualTreeAsset listElementTemplate)
    {
        loadAllCharactorData();
        // Store a reference to the template for the list entries
        m_ListEntryTemplate = listElementTemplate;

        // Store a reference to the character list element
        m_CharacterList = root.Q<ListView>("CharacterList");

        // Store references to the selected character info elements
        m_CharClassLabel = root.Q<Label>("CharacterClass");
        m_CharNameLabel = root.Q<Label>("CharacterName");
        m_CharPortrait = root.Q<VisualElement>("CharacterPortrait");

        // Store a reference to the select button
        m_SelectCharButton = root.Q<Button>("SelectCharButton");
        FillCharacterList();
        m_CharacterList.selectionChanged += OnCharacterSelected;
    }
    void OnCharacterSelected(IEnumerable<object> selectedItems)
    {
        // Get the currently selected item directly from the ListView
        var selectedCharacter = m_CharacterList.selectedItem as CharacterData;
        // Handle none-selection (Escape to deselect everything)
        if (selectedCharacter == null)
        {
            // Clear
            m_CharClassLabel.text = "";
            m_CharNameLabel.text = "";
            m_CharPortrait.style.backgroundImage = null;

            // Disable the select button
            m_SelectCharButton.SetEnabled(false);

            return;
        }
        // Fill in character details
        m_CharClassLabel.text = selectedCharacter.m_Class.ToString();
        m_CharNameLabel.text = selectedCharacter.m_CharacterName;
        m_CharPortrait.style.backgroundImage = new StyleBackground(selectedCharacter.m_PortraitImage);

// Enable the select button
        m_SelectCharButton.SetEnabled(true);
    }

    private void loadAllCharactorData()
    {
        _datas = new List<CharacterData>();
        _datas.AddRange(UnityEngine.Resources.LoadAll<CharacterData>("datas"));
    }

    void FillCharacterList()
    {
        m_CharacterList.makeItem = () =>
        {
            // Instantiate the UXML template for the entry
            var newListEntry = m_ListEntryTemplate.Instantiate();

            // Instantiate a controller for the data
            var newListEntryLogic = new CharactorListEntryController();

            // Assign the controller script to the visual element
            newListEntry.userData = newListEntryLogic;

            // Initialize the controller script
            newListEntryLogic.setVisualElement(newListEntry);

            // Return the root of the instantiated visual tree
            return newListEntry;
        };
        m_CharacterList.bindItem = (element, index) =>
        {
            (element.userData as CharactorListEntryController).setCharactorData(_datas[index]);
        };
        
        // Set a fixed item height
        m_CharacterList.fixedItemHeight = 45;

// Set the actual item's source list/array
        m_CharacterList.itemsSource = _datas;
    }
}